package com.bjdd.jdy.jpush;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.bjdd.anxinqiclient.dao.NewsBeanDao;
import com.bjdd.jdy.Constant;
import com.bjdd.jdy.base.RxManager;
import com.bjdd.jdy.entity.result.JpushResult;
import com.bjdd.jdy.ui.MainActivity;
import com.bjdd.jdy.ui.login.LoginActivity;
import com.bjdd.jdy.util.GreenDaoUtils;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.T;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/4/14.
 */

public class MyReceiver extends BroadcastReceiver {
    private NotificationManager nm;
    @Override
    public void onReceive(Context context, Intent intent) {
        if (nm == null){
            nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        }
        Bundle bundle = intent.getExtras();
        L.d("onReceive - "+intent.getAction());
        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())){
            L.d("JPush用户注册成功");
        }else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())){
            L.d("接受到推送下来的自定义消息");
            processCustomMessage(context,bundle);
        }else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())){
            L.d("接受到推送下来的通知");
            receivinNotification(context,bundle);
        }else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())){
            L.d("用户点击打开通知");
            openNotification(context,bundle);
        }else {
            L.d("Unhandled intent - "+intent.getAction());
        }
    }
    private void receivinNotification(Context context,Bundle bundle){
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        L.d("title : "+title);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        L.d("message ; "+message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        L.d("extras : "+extras);
        NewsBeanDao dao = GreenDaoUtils.getSingleTon().getmDaoSession().getNewsBeanDao();

    }
    private void openNotification(Context context,Bundle bundle){
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String myValue = "";
        try {
            JSONObject extrasJson = new JSONObject(extras);
            myValue = extrasJson.optString("myKey");
            L.d("myValue : "+myValue);
        } catch (Exception e) {
           L.d( "Unexpected: extras is not a valid json"+e);
            return;
        }
        Intent mIntent = new Intent(context, MainActivity.class);
        mIntent.putExtras(bundle);
        mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(mIntent);
//        if (TYPE_THIS.equals(myValue)) {
//            Intent mIntent = new Intent(context, ThisActivity.class);
//            mIntent.putExtras(bundle);
//            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(mIntent);
//        } else if (TYPE_ANOTHER.equals(myValue)){
//            Intent mIntent = new Intent(context, AnotherActivity.class);
//            mIntent.putExtras(bundle);
//            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.startActivity(mIntent);
//        }
    }
    private void processCustomMessage(Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_TITLE);
        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
        //String test =  bundle.getString("test");
        String content = bundle.getString(JPushInterface.EXTRA_EXTRA);
        Gson gson = new Gson();
        JpushResult result = gson.fromJson(content,JpushResult.class);
        int type = result.getType();
        RxManager rxManager = new RxManager();
        L.d("jdy","收到自定义消息："+type);
        L.d("jdy",content);
        switch (type){
            case 0:
                //扫码签收成功
                T.show("签收成功");
                rxManager.post(Constant.SIGN_SCAN_SUCCESS,type);
                break;
            case 1:
                //到付，扫描支付完成
                T.show("支付完成");
                rxManager.post(Constant.PAY_SUCCESS,type);
                break;
        }
       /* if (TextUtils.isEmpty(title)) {
            L.d("Unexpected: empty title (friend). Give up");
            //return;
        }
        L.d("title : "+title);
        L.d("message : "+message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        String myValue = null;
        try {
            JSONObject jsonObject = new JSONObject(extras);
            myValue = jsonObject.getString("myValue");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        L.d("key : "+ myValue);

        boolean needIncreaseUnread = true;*/

//        if (title.equalsIgnoreCase(Config.myName)) {
//            Logger.d(TAG, "Message from myself. Give up");
//            needIncreaseUnread = false;
//            if (!Config.IS_TEST_MODE) {
//                return;
//            }
//        }
//
//        String channel = null;
//        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
//        try {
//            JSONObject extrasJson = new JSONObject(extras);
//            //channel = extrasJson.optString(Constants.KEY_CHANNEL);
//        } catch (Exception e) {
//            L.d( "Unexpected: extras is not a valid json: "+e);
//        }
//
//        // Send message to UI (Webview) only when UI is up
//        if (!Config.isBackground) {
//            Intent msgIntent = new Intent(MainActivity.MESSAGE_RECEIVED_ACTION);
//            msgIntent.putExtra(Constants.KEY_MESSAGE, message);
//            msgIntent.putExtra(Constants.KEY_TITLE, title);
//            if (null != channel) {
//                msgIntent.putExtra(Constants.KEY_CHANNEL, channel);
//            }
//
//            JSONObject all = new JSONObject();
//            try {
//                all.put(Constants.KEY_TITLE, title);
//                all.put(Constants.KEY_MESSAGE, message);
//                all.put(Constants.KEY_EXTRAS, new JSONObject(extras));
//            } catch (JSONException e) {
//            }
//            msgIntent.putExtra("all", all.toString());
//
//            context.sendBroadcast(msgIntent);
//        }
//
//        String chatting = title;
//        if (!StringUtils.isEmpty(channel)) {
//            chatting = channel;
//        }
//
//        String currentChatting = MyPreferenceManager.getString(Constants.PREF_CURRENT_CHATTING, null);
//        if (chatting.equalsIgnoreCase(currentChatting)) {
//            Logger.d(TAG, "Is now chatting with - " + chatting + ". Dont show notificaiton.");
//            needIncreaseUnread = false;
//            if (!Config.IS_TEST_MODE) {
//                return;
//            }
//        }
//
//        if (needIncreaseUnread) {
//            unreadMessage(title, channel);
//        }
//
//        NotificationHelper.showMessageNotification(context, nm, title, message, channel);
    }

}
